/**
 * @author zhoutao
 *  2010-11-6
 */
package com.jlt.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.jlt.entity.dic.DicEntity;
import com.jlt.service.dic.DicEntityManager;

/**
 * @author zhoutao
 * 
 */
@Component
public class DicSelectTag extends BaseTag {

	private static final long serialVersionUID = 1L;

	private String dicTypeCode;
	private String id;
	private Long defaultValue;
	private Boolean nullOption=false;
	private String attribute;

	private DicEntityManager dicEntityManager;

	@Override
	public int doStartTag() throws JspException {
		StringBuffer DicSelect = new StringBuffer();
		
		dicEntityManager = (DicEntityManager) getManagerBean("dicEntityManager");

		if (StringUtils.isNotBlank(dicTypeCode)) {
			DicSelect.append("<select");

			if (StringUtils.isNotBlank(id)) {
				DicSelect.append(" id=\"" + id + "\" name=\"" + id +  "\" class=\"combox\">");
			}

			if (StringUtils.isNotBlank(attribute)) {
				DicSelect.append(" " + attribute);
			}
			DicSelect.append(">");

			if (nullOption) {
				DicSelect.append("<option value=\"0\">请选择</option>");
			}

			List<DicEntity> dicEntityList = (List<DicEntity>) dicEntityManager
					.findDicEntityByTypeCode(dicTypeCode);

			if (CollectionUtils.isNotEmpty(dicEntityList)) {
				for (DicEntity dicEntity : dicEntityList) {
					DicSelect.append("<option");
					DicSelect.append(" value=\"" + dicEntity.getId() + "\"");
					
					if (dicEntity.getId().longValue() == defaultValue.longValue()) {
						DicSelect.append(" selected=\"selected=\"");
					}
					DicSelect.append(">" + dicEntity.getName());
					DicSelect.append("</option>");
				}
			}
			DicSelect.append("</select>");

			JspWriter out = pageContext.getOut();
			try {
				out.print(DicSelect);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}

	public String getDicCode() {
		return dicTypeCode;
	}

	public void setDicCode(String dicTypeCode) {
		this.dicTypeCode = dicTypeCode;
	}

	public String getDicTypeCode() {
		return dicTypeCode;
	}

	public void setDicTypeCode(String dicTypeCode) {
		this.dicTypeCode = dicTypeCode;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Long getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(Long defaultValue) {
		this.defaultValue = defaultValue;
	}

	public Boolean getNullOption() {
		return nullOption;
	}

	public void setNullOption(Boolean nullOption) {
		this.nullOption = nullOption;
	}

	public String getAttribute() {
		return attribute;
	}

	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}
	
}
